package com.great.filter;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RequestMapping("/filter")
public class Regesite implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse)response;
        String user_name = req.getParameter("user_Name");
        String user_password = req.getParameter("user_Password");
        String user_nickname= req.getParameter("user_Nickname");
        String user_repassword = req.getParameter("user_rePassword");
        String user_sex = req.getParameter("user_Sex");

        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();

        if(user_name.equals("")){
            out.write("<script>");
            out.write("alert('用户名不能为空');");
            out.write("location.href='reforward';");
            out.write("</script>");
            out.close();
            return;
        }
        else if(user_nickname.equals("")){
            out.write("<script>");
            out.write("alert('昵称不能为空');");
            out.write("location.href='reforward';");
            out.write("</script>");
            out.close();
            return;
        }
        else if(user_sex.equals("")){
            out.write("<script>");
            out.write("alert('性别不能为空');");
            out.write("location.href='reforward';");
            out.write("</script>");
            out.close();
            return;
        }
        else if(user_password.equals("")){
            out.write("<script>");
            out.write("alert('密码不能为空');");
            out.write("location.href='reforward';");
            out.write("</script>");
            out.close();
            return;
        }
        else if(user_repassword.equals("")){
            out.write("<script>");
            out.write("alert('确认密码不能为空');");
            out.write("location.href='reforward';");
            out.write("</script>");
            out.close();
            return;
        }else {
            chain.doFilter(req, response);
        }

    }

    @Override
    public void destroy() {

    }
}
